package com.changgou.task;

import com.alibaba.fastjson.JSON;
import com.changgou.config.RabbitMQConfig;
import com.changgou.order.feign.TaskFeign;
import com.changgou.order.pojo.Task;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import org.slf4j.Logger;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
public class QueryPointTask {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private TaskFeign taskFeign;
    @Autowired
    private Logger logger;

    @XxlJob("queryTask")
    public ReturnT<String> queryTask(String s) throws Exception {

        //1. 获取小于系统当前时间的数据
        List<Task> taskList = taskFeign.find();
        if (taskList != null && taskList.size()>0){
            logger.info(LocalDateTime.now()+"执行添加积分");
            //2.将任务发送到消息队列上
            for (Task task : taskList) {
                rabbitTemplate.convertAndSend(RabbitMQConfig.EX_BUYING_ADDPOINTUSER,RabbitMQConfig.CG_BUYING_ADDPOINT_KEY, JSON.toJSONString(task));
                System.out.println("订单服务向添加积分队列发送了一条消息");
            }
        }else {
            logger.info("无添加积分任务");
        }
        return ReturnT.SUCCESS;
    }
}
